package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.recruitment_management_system.mapper.SysRoleMapper;
import com.team.recruitment_management_system.pojo.constant.SystemConstantEnum;
import com.team.recruitment_management_system.pojo.dto.role.AddRoleDto;
import com.team.recruitment_management_system.pojo.dto.role.ChangeRoleInfoDto;
import com.team.recruitment_management_system.pojo.dto.role.RolePageQuery;
import com.team.recruitment_management_system.pojo.entity.SysRole;
import com.team.recruitment_management_system.pojo.vo.OptionVo;
import com.team.recruitment_management_system.pojo.vo.RoleInfoVo;
import com.team.recruitment_management_system.service.SysRoleService;
import com.team.recruitment_management_system.util.result.PageResult;
import com.team.recruitment_management_system.util.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author 86147
 * @description 针对表【sys_role(角色表)】的数据库操作Service实现
 * @createDate 2024-03-21 20:28:40
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Result<RoleInfoVo> getRoleById(int id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        if (sysRole == null) {
            return Result.build(null, 500, "该角色不存在");
        }
        RoleInfoVo roleInfoVo = new RoleInfoVo();
        BeanUtils.copyProperties(sysRole, roleInfoVo);
        return Result.ok(roleInfoVo);
    }


    @Override
    public PageResult<RoleInfoVo> getRolePage(RolePageQuery rolePageQuery) {
        Page<SysRole> rolePage = new Page<>(rolePageQuery.getPageNum(), rolePageQuery.getPageSize());
        IPage<RoleInfoVo> roleInfoVoIPage = sysRoleMapper.getRolePage(rolePage, rolePageQuery);
        return PageResult.success(roleInfoVoIPage);
    }


    @Override
    public Result<Void> addRole(AddRoleDto addRoleDto) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(addRoleDto, sysRole);
        this.save(sysRole);
        return Result.ok(null, "添加成功");
    }

    @Override
    public Result<Void> updateRole(ChangeRoleInfoDto changeRoleInfoDto) {
        SysRole role = sysRoleMapper.selectById(changeRoleInfoDto.getId());
        if (role == null) {
            return Result.build(null, 500, "角色不存在");
        }
        BeanUtils.copyProperties(changeRoleInfoDto, role);
        this.updateById(role);
        return Result.ok(null, "修改成功");
    }

    @Override
    public Result<Void> changeRoleStatus(Long id, boolean isDisabled) {
        SysRole role = sysRoleMapper.selectById(id);
        if (role == null) {
            return Result.build(null, 500, "角色不存在");
        }
        if (isDisabled) {
            if (Objects.equals(role.getStatus(), SystemConstantEnum.STATUS_DISABLED.getValue())) {
                return Result.build(null, 500, "角色已停用中");
            }
            role.setStatus(SystemConstantEnum.STATUS_DISABLED.getValue());
            this.updateById(role);
            return Result.ok(null, "角色已成功停用");
        } else {
            if (Objects.equals(role.getStatus(), SystemConstantEnum.STATUS_NORMAL.getValue())) {
                return Result.build(null, 500, "角色正处于正常状态");
            }
            role.setStatus(SystemConstantEnum.STATUS_NORMAL.getValue());
            this.updateById(role);
            return Result.ok(null, "角色已恢复到正常状态");
        }

    }


    @Override
    public Result<Void> deleteRole(Long id) {
        SysRole role = sysRoleMapper.selectById(id);
        if (role == null) {
            return Result.build(null, 500, "角色不存在");
        }
        if (Objects.equals(role.getDeleted(), SystemConstantEnum.IS_DELETED.getValue())) {
            return Result.build(null, 500, "角色已删除");
        }
        this.removeById(role);
        return Result.ok(null, "角色已删除");
    }

    @Override
    public Result<List<OptionVo>> getRoleOptions() {
        List<OptionVo> optionVos = sysRoleMapper.getRoleOptions();
        return Result.ok(optionVos);
    }
}




